<template>
    <div class="createPost-container">
        <el-form
        :model="deliveryForm"
        :rules="rules"
        ref="deliveryForm"
        label-width="100px"
        class="demo-deliveryForm"
        >

            <el-form-item label="快递名称" label-width="120px" prop="name">
                <el-input v-model="deliveryForm.name"></el-input>
            </el-form-item>

            <el-form-item label="快递编码" label-width="120px" prop="code">
                <el-input v-model="deliveryForm.code"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitForm('deliveryForm')" :loading="loading">立即更新</el-button>
                <el-button @click="resetForm('deliveryForm')">重置</el-button>
            </el-form-item>

        </el-form>
  </div>
</template>

<script>

import { deliveryEdit, deliveryInfo} from '@/api/delivery.js'

export default {
    name: 'Delivery',
    components: {  },
    data (){
        return {
            loading: false,
            deliveryForm: {
                id: 0,
                name: '',
                code: '',
            },
            rules: {
                name: [{required: true, message: '请填写快递名称', trigger: 'blur'}],
                code: [{required: true, message: '请填写快递编码', trigger: 'blur'}],
            }
        }
    },
    created(){
        // 需要编辑的快递ID
        let id = this.$route.params && this.$route.params.id;
        this.getInfo(id);
    },
    methods: {
        // 获取基础配置信息
        getInfo(id){
            deliveryInfo({id: id}).then(response => {
                if (response.success == true)
                {
                    let item = response.data;

                    this.deliveryForm.id = item.id;
                    this.deliveryForm.name = item.name;
                    this.deliveryForm.code = item.code;
                }
            });
        },
        // 提交
        submitForm(formName) {
            console.log(formName);
            this.$refs[formName].validate(async valid => {
                if (valid) {
                    this.loading = true;

                    // 新增
                    deliveryEdit(this.deliveryForm).then(response => {
                        this.loading = false;
                        if (response.success == true)
                        {
                            this.$notify({
                                title: "成功",
                                message: "快递更新成功",
                                type: "success",
                                duration: 2000
                            });
                            this.$router.go(-1);
                        }
                        else
                        {
                            this.$notify({
                                title: "失败",
                                message: "快递更新失败",
                                type: "error",
                                duration: 2000
                            });
                        }
                    });
                }
                else
                {
                    console.log('submit error');
                    return false;
                }
            });
        },
        // 重置
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
}

</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";

.createPost-container {
  position: relative;
  margin-top: 20px;
  padding: 10px;
  box-sizing: border-box;
  .createPost-main-container {
    padding: 40px 45px 20px 50px;

    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;

      .postInfo-container-item {
        float: left;
      }
    }
  }

  .word-counter {
    width: 40px;
    position: absolute;
    right: 10px;
    top: 0px;
  }
}

.article-textarea ::v-deep {
  textarea {
    padding-right: 40px;
    resize: none;
    border: none;
    border-radius: 0px;
    border-bottom: 1px solid #bfcbd9;
  }
}
</style>
